import {
  BatchBindTagClassReq,
  OptionTagReq,
  TagItem,
} from '@/api/premium/content/model/tagModel'
import {
  batchBindCategory,
  batchDelTagClassRef,
  optionTag,
  TagApi,
} from '@/api/premium/content/tag'
import { useStore } from '@/store'
import { message } from 'ant-design-vue'
import { cloneDeep } from 'lodash-es'
import { computed, reactive, ref } from 'vue'
import { EditTagDiaState, noClassifyItem } from '../types'

const useEditTagDialog = (searchApi: AsyncFun) => {
  const store = useStore()
  const oldTagName = ref<string>('')
  const oldCategoryList = ref<
    {
      label: string
      value: string
    }[]
  >([])
  const editTagDiaState = reactive<EditTagDiaState>({
    visiable: false,
    currentTag: undefined,
    formData: {
      tagName: '',
      categoryIds: [],
    },
  })

  const openEditTagDia = (tag: TagItem) => {
    editTagDiaState.currentTag = tag
    editTagDiaState.formData.tagName = tag.tagName
    if (tag.refList) {
      editTagDiaState.formData.categoryIds = tag.refList.map((item) => {
        return {
          label: item.categoryName,
          value: item.categoryId,
        }
      })
      oldCategoryList.value = cloneDeep(editTagDiaState.formData.categoryIds)
    } else {
      editTagDiaState.formData.categoryIds = [
        {
          label: noClassifyItem.name,
          value: noClassifyItem.id,
        },
      ]
      oldCategoryList.value = []
    }
    oldTagName.value = tag.tagName
    editTagDiaState.visiable = true
  }

  const confirmEditTagDia = async () => {
    if (!editTagDiaState.currentTag) return
    const classEq =
      oldCategoryList.value.map((item) => item.value).toString() ===
      editTagDiaState.formData.categoryIds.map((item) => item.value).toString()
    const tagEq = editTagDiaState.formData.tagName === oldTagName.value
    // 如果改动了分类的值就执行分类修改逻辑
    if (!classEq) {
      if (editTagDiaState.currentTag.refList) {
        //1. 解绑标签所有旧分类
        const desOldTagClassReqData: string[] =
          editTagDiaState.currentTag.refList.map((item) => item.refId)
        if (desOldTagClassReqData.length > 0) {
          await batchDelTagClassRef(desOldTagClassReqData)
        }
      }

      //2. 绑定标签当前选择分类
      const categoryList = editTagDiaState.formData.categoryIds.map(
        (item) => item.value
      )
      if (!categoryList.includes('-1')) {
        const bindTagClassReqData: BatchBindTagClassReq = {
          categoryList,
          relationList: [editTagDiaState.currentTag.id],
        }
        await batchBindCategory(bindTagClassReqData)
      }
    }
    // 如果改动了tag的值就执行修改标签名逻辑
    if (!tagEq) {
      //3. 修改标签信息
      const optionTagReqData: OptionTagReq = {
        id: editTagDiaState.currentTag.id,
        tagName: editTagDiaState.formData.tagName,
      }
      await optionTag(optionTagReqData)
    }
    if (!tagEq || !classEq) {
      message.success('修改成功')
    }
    editTagDiaState.visiable = false
    await searchApi()
  }
  const confirmEditTagDiaLoading = computed(
    () =>
      store.getters['global/isOpLoading'](TagApi.OPTION_TAG) ||
      store.getters['global/isOpLoading'](TagApi.BATCH_BIND_TAG_CLASS) ||
      store.getters['global/isOpLoading'](TagApi.BATCH_DELETE_TAGCLASS_REF)
  )

  return {
    editTagDiaState,
    openEditTagDia,
    confirmEditTagDia,
    confirmEditTagDiaLoading,
  }
}

export default useEditTagDialog
